/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.RegisteredUser;
import com.blog.city.checkthedep.entities.Tag;
import com.blog.city.checkthedep.sessionbeans.ArticleFacadeLocal;
import com.blog.city.checkthedep.sessionbeans.RegisteredUserFacadeLocal;
import com.blog.city.checkthedep.sessionbeans.TagFacadeLocal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ManagedProperty;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;

/**
 *
 * @author Breath
 */
@ManagedBean
@RequestScoped
public class ArticleBean {

    private List<RegisteredUser> availableAuthors;
    private String author = "";
    private List<Article> articles;
    private List<Article> latestArticles;
    private List<Article> articlesByMonth;
    private List<Article> articlesByAuthor;
    private Article selectedArticle = new Article();
    private String articleType = "published";
    private String monthString = "";
    private String tags = "";
    @EJB
    private ArticleFacadeLocal articleFac;
    @EJB
    private RegisteredUserFacadeLocal regFac;
    @EJB
    private TagFacadeLocal tagFac;
    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    /**
     * 
     */
    @PostConstruct
    public void retrieveArticles() {
        this.setArticles(articleFac.findArticlesByStatus(articleType));
        retrieveLatestPusblished();
        retrieveAvailalableAuthors();
        retrieveDate();
    }

    /**
     * 
     * @return
     */
    public String getTags() {
        return tags;
    }

    /**
     * 
     * @param tags
     */
    public void setTags(String tags) {
        this.tags = tags;
    }

    /**
     * 
     * @param loginBean
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    /**
     * 
     * @return 
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * 
     * @return
     */
    public List<Article> getLatestArticles() {
        return latestArticles;
    }

    /**
     * 
     * @param latestArticles
     */
    public void setLatestArticles(List<Article> latestArticles) {
        this.latestArticles = latestArticles;
    }

    /**
     * 
     */
    public void setAuthor() {
        selectedArticle.setRegisteredUserId(loginBean.getCheckedUser());
    }

    /**
     * 
     * @return
     */
    public Article getSelectedArticle() {
        return selectedArticle;
    }

    /**
     * 
     * @param selectedArticle
     */
    public void setSelectedArticle(Article selectedArticle) {
        this.selectedArticle = selectedArticle;
    }

    /**
     * 
     * @return
     */
    public ArticleFacadeLocal getArticleFac() {
        return articleFac;
    }

    /**
     * 
     * @param articleFac
     */
    public void setArticleFac(ArticleFacadeLocal articleFac) {
        this.articleFac = articleFac;
    }

    /**
     * 
     * @return
     */
    public List<Article> getArticles() {
        return articles;
    }

    /**
     * 
     * @param articles
     */
    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    /**
     * 
     */
    public void retrieveLatest() {
        this.setLatestArticles(articleFac.latestArticles());
    }

    /**
     * 
     */
    public void retrieveLatestPusblished() {
        this.setLatestArticles(articleFac.latestPublishedArticles());
    }

    /**
     * 
     */
    public void persistArticle() {
        if (selectedArticle != null) {
            setAuthor();
            String splitTags[] = tags.split(" ");
            List<Tag> tagsForArticle = new LinkedList<Tag>();
            for (int i = 0; i < splitTags.length; i++) {
                Tag tag = new Tag();
                tag.setName(splitTags[i]);
                tagsForArticle.add(tag);
                tagFac.create(tag);
            }
            selectedArticle.setTagList(tagsForArticle);
            articleFac.create(selectedArticle);
            selectedArticle = new Article();
            tags = "";
            retrieveArticles();
        }
    }

    /**
     * 
     */
    public void findByAuthor() {
        this.setArticlesByAuthor(articleFac.findByAuthor(regFac.findRegUserByUsername(author)));
    }

    /**
     * 
     */
    public void findByMonth() {
        Integer month = Integer.parseInt(monthString);
        List<Article> byMonth = articleFac.findArticleBymonth(month);
        List<Article> publishedbyMonth = new LinkedList<Article>();
        for (int i = 0; i < byMonth.size(); i++) {
            if (byMonth.get(i).getStatus().equals("Published")) {
                publishedbyMonth.add(byMonth.get(i));
            }
        }
        this.setArticlesByMonth(publishedbyMonth);
    }

    /**
     * 
     * @return
     */
    public List<Article> getArticlesByAuthor() {
        return articlesByAuthor;
    }

    /**
     * 
     * @param articlesByAuthor
     */
    public void setArticlesByAuthor(List<Article> articlesByAuthor) {
        this.articlesByAuthor = articlesByAuthor;
    }

    /**
     * 
     * @return
     */
    public List<Article> getArticlesByMonth() {
        return articlesByMonth;
    }

    /**
     * 
     * @param articlesByMonth
     */
    public void setArticlesByMonth(List<Article> articlesByMonth) {
        this.articlesByMonth = articlesByMonth;
    }

    /**
     * 
     * @return
     */
    public List<RegisteredUser> getAvailableAuthors() {
        return availableAuthors;
    }

    /**
     * 
     * @param availableAuthors
     */
    public void setAvailableAuthors(List<RegisteredUser> availableAuthors) {
        this.availableAuthors = availableAuthors;
    }

    /**
     * 
     */
    public void retrieveAvailalableAuthors() {
        this.setAvailableAuthors(regFac.findAll());
    }

    /**
     * 
     * @return
     */
    public String getAuthor() {
        return author;
    }

    /**
     * 
     * @param author
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * 
     */
    public void retrieveDate() {
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd");
        String now = formatter.format(currentDate.getTime());
        this.selectedArticle.setDate(currentDate.getTime());
    }

    /**
     * 
     * @return
     */
    public String getMonthString() {
        return monthString;
    }

    /**
     * 
     * @param monthString
     */
    public void setMonthString(String monthString) {
        this.monthString = monthString;
    }
    /*
     * public void onRowSelect(SelectEvent event) {
     * }
     * public void onRowUnselect(UnselectEvent event) {
     * }
     */
}
